/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @BaBaseActivity.java  2014-9-9 ����10:16:36 - Carson
 * @author YanXu
 * @email:981385016@qq.com
 * @version 1.0
 */

package com.android.tiango.baba;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RelativeLayout;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.act.ActBaList;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.ImageLoader;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.view.AppDialog;
import com.android.tiango.baba.view.AppTitleBar;
import com.android.tiango.baba.view.WaitingDialog;

public class BaBaseActivity extends FragmentActivity {

	protected RelativeLayout mLoading;
	protected AppTitleBar mTitleBar;
	BroadcastReceiver mBroadcastReceiver;
	protected ImageLoader mImageLoader;
	private WaitingDialog mWaitingDialog;

	// private WaitingDialog mWaitingDialog;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		if (mBroadcastReceiver == null) {
			mBroadcastReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					finish();
				}
			};
			IntentFilter filter = new IntentFilter();
			filter.addAction(getApplicationContext().getPackageName()
					+ Constants.ACTION_EXIT_SYSTEM);
			this.registerReceiver(mBroadcastReceiver, filter);
		}

	}

	public void showDialogMessage(String text) {

		AppDialog.Builder ibuilder = new AppDialog.Builder(this);
		ibuilder.setMessage(text);
		ibuilder.setPositiveButton(R.string.know, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		ibuilder.create().show();
	}


	protected void setBackOnClickListener() {
		if (mTitleBar != null) {
			mTitleBar.setBackIcon(R.drawable.icon_back);
			mTitleBar.setBackOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					finish();
				}
			});
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mBroadcastReceiver != null) {
			this.unregisterReceiver(mBroadcastReceiver);
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		this.finish();
	}

	public void setTitleName(CharSequence title) {
		if (mTitleBar == null) {
			mTitleBar = (AppTitleBar) findViewById(R.id.titlebar);
		}
		if (mTitleBar != null) {
			mTitleBar.setTitle(title);
		}
	}

	public void showActivity(Class<?> classz, boolean finish) {
	startActivity(new Intent(this, classz));
		if (finish) {
			this.finish();
		}
	}

	public void showActivity(Class<?> classz, String data) {
		Intent it = new Intent(this, classz);
		it.putExtra(Constants.DATA, data);
		startActivity(it);
	}

	public void showActivity(Class<?> classz, int data) {
		Intent it = new Intent(this, classz);
		it.putExtra(Constants.DATA, data);
		startActivity(it);
	}

	/**
	 * Fragment跳转
	 * @param fm
	 * @param fragmentClass
	 * @param tag
	 * @param args
	 */
	private void turnToFragment(FragmentManager fm, Class<? extends Fragment> fragmentClass,
			String tag, Bundle args) {
		Fragment fragment = fm.findFragmentByTag(tag);
		boolean isFragmentExist = true;
		if (fragment == null) {
			try {
				isFragmentExist = false;
				fragment = fragmentClass.newInstance();
				fragment.setArguments(new Bundle());
			} catch (java.lang.InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		if (fragment.isAdded()) {
			return;
		}
		if (args != null && !args.isEmpty()) {
			fragment.getArguments().putAll(args);
		}
		FragmentTransaction ft = fm.beginTransaction();
		// ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
		// android.R.anim.fade_in, android.R.anim.fade_out);
		// ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
		ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
				android.R.anim.fade_in, android.R.anim.fade_out);
		if (isFragmentExist) {
			ft.replace(R.id.ll_frament, fragment);
		} else {
			ft.replace(R.id.ll_frament, fragment, tag);
		}

		ft.addToBackStack(tag);
		// ft.commitAllowingStateLoss();
		ft.commit();
	}

	protected void turnToFragment(Class<? extends Fragment> fragmentClass) {
		turnToFragment(getSupportFragmentManager(), fragmentClass, fragmentClass.getSimpleName(),
				null);
	}
	public void showWaitingDialog() {
		if (mWaitingDialog == null) {
			mWaitingDialog = new WaitingDialog(this);
			// mWaitingDialog = new ProgressDialog(this);
			mWaitingDialog.setCanceledOnTouchOutside(false);
			// mWaitingDialog.setMessage(getString(R.string.action_waiting));
			mWaitingDialog.setCancelable(true);
		}
		if (!this.isFinishing() && !mWaitingDialog.isShowing()) {
			mWaitingDialog.show();
		}
	}
	public void showWaitingDialog(boolean isCancel) {
		if (mWaitingDialog == null) {
			mWaitingDialog = new WaitingDialog(this);
			// mWaitingDialog = new ProgressDialog(this);
			mWaitingDialog.setCanceledOnTouchOutside(false);
			// mWaitingDialog.setMessage(getString(R.string.action_waiting));
			mWaitingDialog.setCancelable(isCancel);
		}
		if (!this.isFinishing() && !mWaitingDialog.isShowing()) {
			mWaitingDialog.show();
		}
	}

	public void showWaitingDialog(String msg) {
		if (mWaitingDialog == null) {
			mWaitingDialog = new WaitingDialog(this);
			// mWaitingDialog = new ProgressDialog(this);
			mWaitingDialog.setCanceledOnTouchOutside(false);
			// mWaitingDialog.setMessage(getString(R.string.action_waiting));
			mWaitingDialog.setCancelable(true);
		}
		mWaitingDialog.setMessage(msg);
		if (!this.isFinishing() && !mWaitingDialog.isShowing()) {
			mWaitingDialog.show();
		}
	}

	public void dismissWaitingDialog() {
		if (mWaitingDialog != null && mWaitingDialog.isShowing() && !this.isFinishing()) {
			mWaitingDialog.dismiss();
		}
	}
	/**
	 * 根据id获取string中的数据
	 * @param id
	 * @return
	 */
	protected String getStringValue(int id) {
		String value = BaBaseApplication.getAppContext().getResources().getString(id);
		return StringUtils.isEmpty(value) ? "" : value;
	}
}
